package org.dromara.web.service.business;


import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.dao.business.domain.GreenWord;
import org.dromara.dao.business.domain.GreenWordQuery;
import org.dromara.dao.business.mapper.GreenWordMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;

@Service
public class GreenWordService {

    @Autowired
    GreenWordMapper greenWordMapper;


    public TableDataInfo<GreenWord> queryPageList(GreenWordQuery query, PageQuery pageQuery) {
        return new TableDataInfo(
                greenWordMapper.paginationSelectList(query, pageQuery),
                greenWordMapper.paginationSelectCount(query)
        );
    }


    public GreenWord queryById(Long id) {
        return greenWordMapper.selectById(id);
    }


    @Transactional
    public GreenWord insert(GreenWord entity) {
        greenWordMapper.insert(entity);
        return entity;
    }

    @Transactional
    public void updateById(GreenWord update) {

        GreenWord entity = greenWordMapper.selectById(update.getId());
        if (entity == null) {
            throw new ServiceException();
        }
        entity.setUserId(update.getUserId());
        entity.setUserName(update.getUserName());
        entity.setContent(update.getContent());
        entity.setPass(update.getPass());
        entity.setIpAddress(update.getIpAddress());
        entity.setUpdateTime(LocalDateTime.now());
        greenWordMapper.updateById(entity);
    }


    @Transactional
    public void deleteById(Long id) {
        greenWordMapper.deleteById(id);
    }

    @Transactional
    public void deleteByIds(Long[] ids) {
        greenWordMapper.deleteByIds(Arrays.asList(ids));
    }
}
